<?
/**
 * @version zhou1.1
 * @author :z
 * @uses dealwith community str,array etc..progrese
 * @see http://top369.cn
 * @time :20100818
 */

class z {
	static $temp; //php5版本 
	static $_z;
	static function init() {
		$_z = new self ();
		return $_z;
	}
	
	function zencryption($pwd) {
		$len = strlen ( $pwd );
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		$bc = substr ( $chars, $len, 2 );
		$sc = substr ( $chars, - $len, 2 );
		$spechars = "!@#$%^&*()";
		$spechar = substr ( $spechars, substr ( $len, - 1, 1 ), 2 );
		
		return $pwd . $bc . $sc . $spechar . ($len * 8);
	}
	
	function islocal($temar) {
		$ip = $_SERVER ["REMOTE_ADDR"];
		
		if (! $ip)
			return true;
		if ('' == $temar) {
			$ipstrar = array ('ipa' => '192.xxx.xxx.xxx', 'ip2' => '191.xx.xx.xx', 'ip3' => '127.xx.xx.xx', 'default' => '' );
		} else {
			$ipstrar = array ();
			foreach ( $temar as $key => $temv ) {
				if ('default' == $key)
					continue;
				$ipstrar [] = substr ( $temv, 0, 3 );
			}
		}
		//
		foreach ( $ipstrar as $ipstrv ) {
			if (0 === $pos = strpos ( $ip, $ipstrv )) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 
	 * 获得类的所有方法 ...
	 * @param class $obj 
	 */
	function class_methods($obj) {
		$r = new ReflectionClass ( $obj );
		$Methodsar = $r->getMethods ();
		
		return $Methodsar;
	}
	/**
	 * 
	 * Enter description here ...
	 * @param str $str ：分析的文本对象
	 * @param array $tipar  ：分隔符号多符号
	 * @ return $partar 
	 */
	function getpartfromstr($str, $tipar = array(',','，')) {
		if ('' == $str)
			return '';
		$str = str_replace ( $tipar, ' ^~# ', $str );
		$partar = explode ( ' ^~# ', $str );
		foreach ( $partar as $partk => $partv ) {
			if ($partv == '')
				unset ( $partar [$partk] );
		
		}
		return $partar;
	
	}
	
	function getbrowse() {
		if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "MSIE 8.0" ))
			$HTTP_USER_AGENT = "ie8";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "MSIE 7.0" ))
			$HTTP_USER_AGENT = "ie7";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "MSIE 6.0" ))
			$HTTP_USER_AGENT = "ie6";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "Firefox/3" ))
			$HTTP_USER_AGENT = "Firefox3";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "Firefox/2" ))
			$HTTP_USER_AGENT = "Firefox2";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "Chrome" ))
			$HTTP_USER_AGENT = "Chrome";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "Safari" ))
			$HTTP_USER_AGENT = "Safari";
		else if (strpos ( $_SERVER ["HTTP_USER_AGENT"], "Opera" ))
			$HTTP_USER_AGENT = "Opera";
		else
			$HTTP_USER_AGENT = $_SERVER ["HTTP_USER_AGENT"];
		
		return $HTTP_USER_AGENT;
	}
	
	function region($ar) {
		$k = key ( $ar );
		$v = $ar [$k];
		eval ( "\$this->$k = $v" );
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param str $path ：相对路径
	 * @param str $str  ：写入文本
	 */
	function makefile($path, $str) {
		$fp = fopen ( $path, 'wb+' );
		fwrite ( $fp, $str );
		fclose ( $fp );
	
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param str $path
	 * @param str|array $str
	 */
	function wlog($path, $str) {
		if (is_array ( $str )) {
			foreach ( $str as $strk => $strv ) {
				$str .= " $strk=>$strv\r\n ";
			}
		
		} else {
			$str .= "\r\n";
		}
		if ($path == '') {
			$path .= date ( 'Y-m-d', time () ) . 'log.txt';
		} else {
			$path = $path . date ( 'Y-m-d', time () ) . 'log.txt';
		}
	  
			$fp = fopen ( $path, 'a+' );
		   //$str = str_replace ( '[]', '', trim($str) );
		   $re = fwrite ( $fp, $str );
		   fclose ( $fp );
		
	
	}
	
	/**
	 * 
	 * delete log from path 
	 * @param str $path
	 */
	function deletelog($path, $name = '') {
		$f = $name == '' ? $path . '/' . date ( 'Y-m-d', time () ) . 'log.txt' : $path . '/' . $name;
		
		if (file_exists ( $f )) {
			$fp = fopen ( $f, 'w' );
			fwrite ( $fp, ' ' );
			fclose ( $fp );
			return true;
		} else
			return false;
	
	}
	
	/**
	 * return array that come from this path file explode by \r\n
	 * @param unknown_type $path
	 */
	function readtxttoar($path) {
		if (! file_exists ( $path ))
			return false;
		$stram = file_get_contents ( $path );
		$stram = preg_replace ( '/\\r\\n/', '|=|', $stram );
		$stramar = explode ( '|=|', $stram );
		$stramar = $this->Getvalueablearray ( $stramar );
		if (is_array ( $stramar ) && count ( $stramar ) != 0) {
			return $stramar;
		} else
			return false;
	}
	/**
	 * checkserver
	 * @name checkserver
	 * @param $server
	 * @param $port
	 */
 
	function checkserver($server,$port){
		$fp = fsockopen("$server", $port, $errno, $errstr, 1);
		
		if($fp) return TRUE;
		return FALSE;
	
	}
	
	/**
	sentdata
	
	*/
	function sentdata($server, $port = 9005, $sent) {
		include_once 'socketclientclass.php';
		try {
			@$Connection = new socket ( $server, $port );
			@$Connection->Connect ();
			@$Connection->send ( $sent );
			@$Connection->close ();
		} catch ( Exception $e ) {
			return FALSE;
		}
		return TRUE;
	}
	
	
	/**
	 * sent and get reback
	 * @name getsocketback
	 * @param $server
	 * @param $port
	 * @param $sent
	 */
	function getsocketback($server, $port = 9005, $sent) {
		include_once 'socketclientclass.php';
		try {
			@$Connection = new socket ( $server, $port );
			@$Connection->Connect ();
			@$Connection->send ( $sent );
			@$back = $Connection->read ();
			@$Connection->close ();
		} catch ( Exception $e ) {
			$back = false;
			continue;
		}
		return $back;
	
	}
	
	
	
	/**
	 * 
	 * add configar
	 * @param unknown_type $file
	 * @param unknown_type $rank
	 * @param array $array eg: you need array('db_config','tb','webconfig' relase array
	 */
	public function loadconfigarray($file, $path = null, $array = array('db_config','tb','webconfig')) {
		$temp = $path . '/' . "$file" . ".php";
		if (file_exists ( $temp )) {
			require_once $temp;
			foreach ( $array as $v ) {
				$tempar ["$v"] = $$v;
			}
			return $tempar;
		} else {
			exit ( $file . ' file is not exits' );
		}
	
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param str $class eg:classname
	 * @param str $rank  eg:../
	 */
	function loadclass($class, $rank = null) {
		$rank = $rank == null ? '../' : $rank;
		$temp = $rank . 'core/' . "$class" . ".php";
		if (file_exists ( $temp )) {
			require_once $temp;
			$temp = new $class ();
			return $temp;
		} else {
			exit ( $class . ' file is not exits' );
		}
	
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $model : name
	 * @param $rank  : ../ 
	 */
	function m($model, $rank = null) {
		$rank == null ? $rank = '../' : $rank = $rank;
		$file = $rank . 'model/' . "$model" . "model.php";
		if (file_exists ( $file )) {
			require_once $rank . 'model/' . "$model" . "model.php";
			$temp = $model . "model";
			$temp = new $temp ();
			return $temp;
		} else {
			exit ( ' file is not exits' );
		}
	
	}
	
	/**
	 * 
	 * @param $File
	 * @param $uploaddir
	 */
	function uploadImage($File, $uploaddir) {
		$time = time () . rand ( 1000, 9999 );
		$filterar = array ('.', ',', '-', '_', ':', '?', '<', '>', '|' );
		
		$uploadfile = str_replace ( $filterar, '', substr ( basename ( $File ["name"] ), 0, - 5 ) );
		$uploadfile = $uploaddir . '/' . $uploadfile . $time . substr ( basename ( $File ["name"] ), - 5 );
		
		$temploadfile = $File ["tmp_name"];
		if (@move_uploaded_file ( $temploadfile, $uploadfile )) {
			return $uploadfile;
		} else {
			return false;
		}
	
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param array $q
	 * @attion somedefault word
	 */
	function filterrequest($q) {
		$filterar = array ('select', 'SELECT', 'serch', 'SERCH', 'DELETE', 'script', 'SCERIPT', 'create', 'CREATE' );
		if (is_array ( $q ) && count ( $q ) != 0) {
			foreach ( $q as $qk => $qv ) {
				$q [$qk] = str_replace ( $filterar, '', $qv );
			}
			return $q;
		}
	}
	
	/**
	 * get url;
	 * @param 
	 */
	function getCurrentUrl() {
		$currenturl = '';
		$currenturl .= (isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] != 'off') ? 'https://' : 'http://';
		//$currenturl .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		if (! isset ( $_SERVER ['REQUEST_URI'] )) {
			$_SERVER ['REQUEST_URI'] = substr ( $_SERVER ['argv'] [0], strpos ( $_SERVER ['argv'] [0], ';' ) + 1 );
			$_SERVER ['REQUEST_URI'] = $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['argv'] [0];
		}
		if ($_SERVER ['REQUEST_URI'] == '?') {
			$_SERVER ['REQUEST_URI'] = '';
		}
		$currenturl .= $_SERVER ['SERVER_NAME'] . $_SERVER ['REQUEST_URI'];
		return $currenturl;
	}
	
	/**
	 * 
	 * @param $val
	 */
	function clean_value($val) {
		if ($val == '') {
			return '';
		}
		$val = str_replace ( '&#032;', ' ', $val );
		$val = str_replace ( '&', '&amp;', $val );
		$val = str_replace ( '<!--', '&#60;&#33;--', $val );
		$val = str_replace ( '-->', '--&#62;', $val );
		$val = preg_replace ( '/<script/i', '&#60;script', $val );
		$val = str_replace ( '>', '&gt;', $val );
		$val = str_replace ( '<', '&lt;', $val );
		$val = str_replace ( '\'', '&quot;', $val );
		$val = preg_replace ( '/\|/', '&#124;', $val );
		$val = preg_replace ( '/&lt;br&gt;/', '<br>', $val );
		$val = preg_replace ( '/\n/', '<br>', $val );
		$val = preg_replace ( '/\\\$/', '&#036;', $val );
		$val = preg_replace ( '/\r/', '', $val );
		$val = str_replace ( '!', '&#33;', $val );
		$val = str_replace ( '', '&#39;', $val );
		$val = stripslashes ( $val );
		$val = preg_replace ( '/\\\/', '&#092;', $val );
		return $val;
	}
	
	/**
	 * rm rewriter one 
	 * @param $array
	 */
	function delSuperArray(&$array) {
		$i = 0;
		while ( isset ( $array [$i] ) ) {
			$j = $i + 1;
			while ( isset ( $array [$j] ) ) {
				if ($array [$i] == $array [$j]) {
					delonArray ( $array, $j );
					$j --;
				}
				$j ++;
			}
			$i ++;
		}
	}
	/**
	 * 
	 * @param $msg
	 * @param $url
	 */
	function Alert($msg, $url = '') {
		$header = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
			<html xmlns='http://www.w3.org/1999/xhtml'>
			<head>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
		if ($url == '') {
			echo "$header
			<script language='javascript'  type='text/javascript'>alert('$msg');history.go(-1);</script>
			</head>
			</html>";
			exit ();
		} else {
			if ($url == "exit") {
				echo "$header
			<script language='javascript'  type='text/javascript'>alert('$msg');window.close();</script>
			</head>
			</html>";
			} else {
				echo "$header
			<script language='javascript'  type='text/javascript'>alert('$msg');document.location.href='$url';</script>
			</head>
			</html>";
			}
			exit ();
		}
	}
	/**
	 * @param $url nessary
	 */
	function jump_url($url = '', $action = '') {
		if ($action == "") {
			$url = "document.location.href = '$url'";
		} elseif ($action == "close") {
			$url = "window.close()";
		} else {
			$url = "history.go(-1)";
		}
		echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
			<html xmlns='http://www.w3.org/1999/xhtml'>
			<head>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
			<script language='javascript'  type='text/javascript'>$url;</script>
			</head>
			</html>";
		exit ();
	}
	/**
	 */
	function lastput($Str) {
		$Str = $this->MinusSpace ( $Str );
		
		switch ($Str) {
			case substr ( $Str, - 2, 2 ) == ',,' :
				return substr ( $Str, 0, - 1 );
				break;
			case substr ( $Str, - 1, 1 ) == ',' :
				return $Str;
				break;
			case substr ( $Str, - 1, 1 ) != ',' :
				return $Str . ',';
				break;
		}
	
	}
	
	function _setcookie($key, $value, $expire = 0, $cookie_path = COOKIE_PATH, $cookie_domain = COOKIE_DOMAIN) {
		setcookie ( $key, $value, $expire, $cookie_path, $cookie_domain );
	}
	
	function is_time($time) {
		$pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
		
		return preg_match ( $pattern, $time );
	}
	
	function is_email($user_email) {
		$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
		if (strpos ( $user_email, '@' ) !== false && strpos ( $user_email, '.' ) !== false) {
			if (preg_match ( $chars, $user_email )) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	function _json_encode($value) {
		if (CHARSET == 'utf-8' && function_exists ( 'json_encode' )) {
			return json_encode ( $value );
		}
		
		$props = '';
		if (is_object ( $value )) {
			foreach ( get_object_vars ( $value ) as $name => $propValue ) {
				if (isset ( $propValue )) {
					$props .= $props ? ',' . $this->_json_encode ( $name ) : $this->_json_encode ( $name );
					$props .= ':' . $this->_json_encode ( $propValue );
				}
			}
			return '{' . $props . '}';
		} elseif (is_array ( $value )) {
			$keys = array_keys ( $value );
			if (! empty ( $value ) && ! empty ( $value ) && ($keys [0] != '0' || $keys != range ( 0, count ( $value ) - 1 ))) {
				foreach ( $value as $key => $val ) {
					$key = ( string ) $key;
					$props .= $props ? ',' . $this->_json_encode ( $key ) : $this->_json_encode ( $key );
					$props .= ':' . $this->_json_encode ( $val );
				}
				return '{' . $props . '}';
			} else {
				$length = count ( $value );
				for($i = 0; $i < $length; $i ++) {
					$props .= ($props != '') ? ',' . $this->_json_encode ( $value [$i] ) : $this->_json_encode ( $value [$i] );
				}
				return '[' . $props . ']';
			}
		} elseif (is_string ( $value )) {
			//$value = stripslashes($value);
			$replace = array ('\\' => '\\\\', "\n" => '\n', "\t" => '\t', '/' => '\/', "\r" => '\r', "\b" => '\b', "\f" => '\f', '"' => '\"', chr ( 0x08 ) => '\b', chr ( 0x0C ) => '\f' );
			$value = strtr ( $value, $replace );
			if (CHARSET == 'big5' && $value {strlen ( $value ) - 1} == '\\') {
				$value = substr ( $value, 0, strlen ( $value ) - 1 );
			}
			return '"' . $value . '"';
		} elseif (is_numeric ( $value )) {
			return $value;
		} elseif (is_bool ( $value )) {
			return $value ? 'true' : 'false';
		} elseif (empty ( $value )) {
			return '""';
		} else {
			return $value;
		}
	}
	
	/**
	 * read in a file
	 *
	 * @param string $filename
	 * @return string
	 */
	function _read_file($filename) {
		if (file_exists ( $filename ) && ($fd = @fopen ( $filename, 'rb' ))) {
			$contents = '';
			while ( ! feof ( $fd ) ) {
				$contents .= fread ( $fd, 8192 );
			}
			fclose ( $fd );
			return $contents;
		} else {
			return false;
		}
	}
	
	function MakeHtmlFile($file_name, $c) { //           
		if (! $fp = fopen ( $file_name, "wa" )) {
			echo "fail";
			return false;
		}
		if (! fwrite ( $fp, $c )) {
			echo 'success';
			fclose ( $fp );
			return false;
		}
		fclose ( $fp );
	}
	
	/**
	 * wrapper for eval() retaining $this
	 * @return mixed
	 */
	function _eval($code, $params = null) {
		return eval ( $code );
	}
	
	/**
	 * 
	 *@name znow
	 * @var ''
	 */
	function _date() {
		return date ( "Y-m-d H:is", time () );
	}
	
	/**
	 * 
	 *@name : goto
	 * 
	 */
	function GotoUrl($url) {
		header ( "Location: $url" );
	}
	
	/**
	 *@author: z
	 *@uses :  Minus Space;
	 *@var:$Str [minus \r\n]
	 */
	function MinusSpace($Str) {
		$Str = preg_replace ( '/\s{1,}/', '', $Str );
		return $Str;
	}
	
	/**
	 * @author :z
	 * @uses :return avalue array
	 * 
	 */
	function Getvalueablearray($array) {
		$ArDPhonenum = array ();
		foreach ( $array as $getvalueablek => $getvalueablev ) {
			if (! $getvalueablev) {
				unset ( $array [$getvalueablek] );
			}
		
		}
		return $array;
	
	}
	
	function Actions($f) {

		if (function_exists ( "$f" )) {
			eval ( "$f();" );
			exit ();
		} else {
			$this->Alert ( 'function isnot found!' );
		}
	
	}

}

$z = z::init ();